/*
* dataview 公有方法， 可以供外部调用的
* */
const dvFunPublic = {
  /************************通用操作************************/
  /**设置查询条件
   * @param wrapper 查询对象
   * @param autoReload 自动查询数据
   */
  setQueryWrapper(wrapper, autoReload = true) {
    this.queryWrapper = wrapper
    if (autoReload) {
      this.tablePage.currentPage = 1
      this.fetchData()
    }
  },

  /**设置一次性视图条件 【永久起作用】
   * @param wrapper 查询对象
   * @param autoReload 自动查询数据
   */
  setQueryWrapperOnce(wrapper, autoReload = true) {
    this.queryWrapperOnce = wrapper
    if (autoReload) {
      this.fetchData()
    }
  },
  /**设置按钮隐藏或者显示
   * @param type 按钮类型 view：视图，viewForm：表单，flow：流程，row：行内
   * @param key 按钮标识
   * @param hidden true:隐藏【默认】,false:显示
   */
  setButtonInvisible(type, key, hidden = true) {
    let buttonFound = null
    if (type === 'view') {
      buttonFound = this.viewButtons.find((button) => button.key === key)
    } else if (type === 'viewForm') {
      buttonFound = this.viewFormButtons.find((button) => button.key === key)
    } else if (type === 'flow') {
      buttonFound = this.viewFormFlowButtons.find((button) => button.key === key)
    } else if (type === 'row') {
      buttonFound = this.rowButtons.find((button) => button.key === key)
    }
    if (buttonFound) {
      this.$nextTick(() => {
        this.$set(buttonFound, 'hidden', hidden)
      })
    }
  },

  /**设置原始数据第一行
   * @param row 数据对象
   */
  setRow0(row) {
    row.dirty = false
    const index = this.rows0.findIndex((item) => item[this.primaryKey] == row[this.primaryKey])
    this.rows0[index] = global.utils.jsonClone(row)
  },

  /**设置原始数据与显示数据的第一行
   * @param row 数据对象
   */
  setRow(row) {
    const row0Item = this.rows0.find((item) => item[this.primaryKey] == row[this.primaryKey])
    const rowIndex = this.rows.findIndex((item) => item[this.primaryKey] == row[this.primaryKey])
    this.rows[rowIndex] = global.utils.jsonClone(row0Item)
  },

  //获得主键名称
  getPrimaryKey() {
    return this.primaryKey
  },

  //获取原始数据列表
  getRows0() {
    return this.rows0
  },

  //获取当前显示的行数据（页面搜索过滤之后的）
  getRows() {
    return this.rows
  },

  //获取所有列数据
  getColums() {
    return this.columns
  },

  //获取所有数据字典列表
  getDict() {
    return this.viewFormDictionaries
  },
  /**给数据字典赋值
   * @param key 数据字典标识
   * @param options 数据列表
   */
  pushViewKeyOptions(key, options) {
    this.viewFormDictionaries[key] = options
  },

  /**删除操作的提醒文字
   * @param val 提醒信息
   */
  setDeletePrompt(val) {
    this.deletePrompt = val
  },

  //添加
  ocAddnew() {
    this.addnew(true)
  },

  //刷新数据
  ocRefresh() {
    this.fetchData()
  },

  //刷新数据字典
  ocFetchDict() {
    return this.fetchDict()
  },

  //隐藏所有按钮，只保留关闭
  ocHideFormButtons() {
    this.viewFormButtonVisible = false
  },

  //插入新行
  ocInsertNewRow(row) {
    this.insertEvent(row)
  },

  //设置行选中或者取消选中
  ocSetCheckboxRow(row, checked) {
    this.$refs.tbMain.setCheckboxRow(row, checked)
  },

  ocGetSelectedRows() {
    return this.selectedRows
  },
  //使表格获取焦点
  ocFocus() {
    this.$refs.tbMain.focus()
  },

  /************************表格操作************************/
  /**打开某一行数据
   * @param pk 数据主键
   * @readonly false:只读【默认】,true:可编辑
   */
  viewRow(pk, readonly = false) {
    const cRow = this.getRowByPK(pk)
    if (cRow) {
      this.rowEdit(cRow, readonly)
    }
  },

  /**打开某一行数据 【将追加路由参数进行查询】
   * @param pk 数据主键
   * @readonly false:只读【默认】,true:可编辑
   */
  viewRowOne(pk, readonly = false, pkName = 'id') {
    if (!this.$route.params) this.$route.params = {}
    this.$route.params.autoQuery = true
    this.$route.params[pkName] = pk
    this.fetchData()
  },

  //冲刷新当前行数据
  reloadData(row) {
    const rowIndex = this.rows.findIndex((item) => item[this.primaryKey] == row[this.primaryKey])
    this.rows[rowIndex] = global.utils.jsonClone(row)
    this.$refs.tbMain.loadData(this.rows)
    this.$refs.tbMain.setActiveRow(this.rows[rowIndex])
  },

  //设置活动行
  setCurrent(row) {
    const cRow = row || {}
    this.$set(this, 'currentRow', cRow)
    this.$refs.tbMain && this.$refs.tbMain.setCurrentRow(this.currentRow)
  },

  //异步删除数据
  ocAsynDeleteRow(row) {
    return this.deleteAction(row, null, null)
  },

  //移除行，不调用接口
  ocRemoveRows(rows, noMsg) {
    return this.removeRows(rows, noMsg)
  },

  //移除所有行，不调用接口
  ocRemoveAllRows(noMsg) {
    this.rows = []
    this.rows0 = []
    this.$refs.tbMain.remove()
    noMsg || this.utilMessage.success('删除成功', 500)
    this.utilVue.loadHide()
  },

  //设置复选框隐藏或者显示
  ocSetShowCheckBox(isShow) {
    this.showCheckbox = isShow
  },

  //设置单选框隐藏或者显示
  ocSetShowRadioBox(isShow) {
    this.showRadiobox = isShow
  },

  // 异步保存行，主要应用于网格编辑
  ocAsynSaveRow(row) {
    this.viewFormData = row
    return this.viewFormSave()
  },

  /*************************表单操作************************/
  //获取编辑界面的编辑状态
  getViewFormStatus() {
    return this.viewFormStatus
  },

  //设置编辑页面显示或者隐藏 true:显示，flase:隐藏
  setViewFormVisible(state) {
    this.viewFormVisible = state
  },

  //打开表单
  openViewForm(rowData, viewFormReadOnly = false) {
    this.viewFormReadOnly = viewFormReadOnly
    this.setViewFormVisible(true)

    if (rowData == null) {
      this.$set(this, 'viewFormStatus', 'edit')
      this.viewFormVisible = true
      this.$nextTick(() => {
        const {afterMove} = this.viewFormEvents
        if (afterMove) {
          afterMove({}, this.propInitData, this.viewFormStatus)
        }
      })
      return
    }
    this.refreshCurrentRow(rowData, true)
  },

  //打开表单
  openViewFormCustom(rowData, viewFormReadOnly = false) {
    this.viewFormReadOnly = viewFormReadOnly
    this.setViewFormVisible(true)
    this.$set(this, 'viewFormStatus', 'edit')
    this.viewFormVisible = true
    this.$nextTick(() => {
      const {afterMove} = this.viewFormEvents
      if (afterMove) {
        afterMove(rowData)
      }
    })
  },
  //将表单数据刷新到当前行
  ocRefreshCurrentRow() {
    this.refreshCurrentRow(this.viewFormData, false)
  },

  //保存表单-有同步方法，有遮罩层
  async ocAsynSaveViewForm(call) {
    return this.viewFormSave(false, call)
  },

  //获取表格引用对象
  getGrid() {
    if (!this || !this.$refs.tbMain) {
      throw new Error('请在实例化之后再调用本方法【getGrid】')
    }
    return this.$refs.tbMain
  },

  //获取当前选中行
  getCurrentRow() {
    return this.currentRow
  },

  //更新表单数据 只会触发 afterMove
  ocUpdateViewForm(data) {
    Object.keys(data).forEach(key => {
      this.$set(this.viewFormData, key , data[key])
    })
    const {afterMove} = this.viewFormEvents
    this.$set(this, 'viewFormStatus', 'edit')
    if (afterMove) {
      afterMove(this.viewFormData)
    }
  },

  //保存表单-有同步方法，有遮罩层
  ocAsynSaveViewFormOnly(call) {
    return this.viewFormSave(false, call)
  },
}

export default dvFunPublic
